import { http } from "msw";

import { userRest } from "@/api/user";
import { withResponseSuccess } from "@/mocks/util";

function generateAccessToken() {
  // 使用随机字符生成一个模拟的 accessToken
  const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  let accessToken = "";
  for (let i = 0; i < 32; i++) {
    accessToken += characters.charAt(Math.floor(Math.random() * characters.length));
  }
  return accessToken;
}

const login = http.post(
  userRest.login,
  withResponseSuccess<{ uid: number | string; phoneNumber: string; accessToken: string }, {}>(
    () => ({
      uid: "100",
      phoneNumber: "vincent",
      accessToken: generateAccessToken(),
    }),
  ),
);

const logout = http.post(
  userRest.logout,
  withResponseSuccess(() => ({
    data: null,
  })),
);

const checkLogin = http.post(
  userRest.checkLogin,
  withResponseSuccess<{ name: string; uid: number }, {}>(() => ({
    name: "vincent",
    uid: 100,
    phoneNumber: "18250234234",
  })),
);

const getUserInfo = http.post(
  userRest.userInfo,
  withResponseSuccess<{ name: string; uid: number }, {}>(() => ({
    name: "vincent",
    avatar: "https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png",
    email: "antdesign@alipay.com",
    uid: 100,
    access: "admin",
    address: "西湖区工专路 77 号",
    phone: "0752-268888888",
  })),
);

const getUserMenu = http.post(
  userRest.menus,
  withResponseSuccess(() => {
    return [
      {
        id: 800,
        menuName: "Home",
        resourceType: "MENU",
        children: [
          {
            id: 8001,
            menuName: "Dashboard",
            resourceType: "MENU",
            children: [
              {
                id: 80001,
                menuName: "Dashboard",
                resourceType: "MENU",
                router: "/app/home/dashboard",
              },
            ],
          },
        ],
      },
    ];
  }),
);

const userApi = [getUserMenu, getUserInfo, checkLogin, login, logout];

export default userApi;
